package com.quad.innovators.salesease.service.impl.scan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.ScanSettingConverter;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.scan.ScanSettingMapper;
import com.quad.innovators.salesease.model.entity.scan.ScanSetting;
import com.quad.innovators.salesease.model.form.scan.ScanSettingForm;
import com.quad.innovators.salesease.service.scan.ScanSettingService;

import org.springframework.stereotype.Service;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2025/4/15 10:13
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class ScanSettingServiceImpl extends ServiceImpl<ScanSettingMapper, ScanSetting> implements ScanSettingService {

    private final ScanSettingConverter scanSettingConverter;

    @Override
    public List<ScanSettingForm> getList() {
        List<ScanSetting> scanSettingList = this.list();
        return scanSettingList.stream().map(scanSettingConverter::toForm).toList();
    }

    @Override
    public Boolean save(ScanSettingForm form) {
        ScanSetting scanSetting = scanSettingConverter.toEntity(form);
        scanSetting.setCode(SystemCodeUtils.getCode(SystemCodeEnum.SCAN_SETTING));
        return this.save(scanSetting);
    }

    @Override
    public Boolean remove(String code) {
        ScanSetting scanSetting = this.getOne(new QueryWrapper<ScanSetting>().lambda().eq(ScanSetting::getCode, code));
        if (ObjectUtils.isNull(scanSetting)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return this.removeById(scanSetting.getId());
    }
}
